<?php

namespace app\api\model\service;

use think\Model;
class Rebate extends Model
{
    // 表名
    protected $name = 'service_rebate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function getCount($params)
    {
        return self::where($params)->count();
    }

    public static function getNum($params)
    {
        return self::where($params)->sum('num');
    }

    public static function orderRebate($order)
    {
        if(!$order){
            return false;
        }
        if(!$order['skill_id'] && !$order['shop_id'])
        {
            return true;
        }
        
        $order = Order::where('id',$order['id'])->find();
        $goodsName = OrderDetail::where(['order_id'=>$order['id']])->value('name');
        $skillUserId = Skill::where(['id'=>$order['skill_id']])->value('user_id');
        $shopUserId = $order['shop_id']?Shop::where(['id'=>$order['shop_id']])->value('user_id'):'';
        $rebateData = ['from_user_id'=>$order['user_id'],'type'=>0,'order_id'=>$order['id'],'rebatetype'=>1,'memo'=>$goodsName.'订单收益到账','state'=>1];
        // 原代码
        $skillPrice = $rebateData['num'] =$order['skill_id']?self::getRebatePrice($order['skill_percent'],$order['settle_price']):0;
        if($order['shop_id'])
        {   
            // 商户分平台佣金
            $brokerage = self::getRebatePrice($order['shop_percent'],$order['settle_price']);
            
            if($order['skill_id']){
                $rebateData['user_id'] = $skillUserId;
                // 技师分商户佣金
                // $skillPrice = self::getRebatePrice($order['skill_percent'],$order['settle_price']);
                if($skillPrice > 0){
                    UserInfo::shopMoney(+$skillPrice,$skillUserId,$goodsName.'门店订单收益到账',$order['id']);
                    $rebateData['num'] = $skillPrice;
                    $rebateData['memo'] = $goodsName.'门店订单收益到账';
                    self::create($rebateData);
                }
                // 打车补贴
                if($order['act_travel_price']>0)
                {
                    UserInfo::shopMoney(+$order['act_travel_price'],$skillUserId,$goodsName.'订单车费结算到账',$order['id']);
                    $rebateData['rebatetype'] = 2;
                    $rebateData['num'] = $order['act_travel_price'];
                    $rebateData['memo'] = $goodsName.'订单车费结算到账';
                    self::create($rebateData);
                    // 商家不分车费
                    // UserInfo::shopUserMoney(+$order['act_travel_price'],$shopUserId,$goodsName.'订单车费结算到账',$order['id']);
                    // $rebateData['user_id'] = $shopUserId;
                    // $rebateData['type'] = 1;
                    // self::create($rebateData);
                }
            }
            
            // 原代码
            // $shopPrice = $rebateData['num'] = self::getRebatePrice($order['shop_percent'],$order['settle_price']);
            // 商户得到技师分走佣金剩下的佣金
            $shopPrice = bcsub($brokerage, $skillPrice, 2);
            if($shopPrice > 0){
                UserInfo::shopUserMoney(+$shopPrice,$shopUserId,$goodsName.'订单门店收益到账',$order['id']);
                $rebateData['user_id'] = $shopUserId;
                $rebateData['type'] = 1;
                $rebateData['rebatetype'] = 0;
                $rebateData['num'] = $shopPrice;
                $rebateData['memo'] = $goodsName.'订单门店收益到账';
                self::create($rebateData);
            }
        }else{
            
            $skillPrice = $rebateData['num'] =$order['skill_id']?self::getRebatePrice($order['skill_percent'],$order['settle_price']):0;
            $rebateData['rebatetype'] = 0;
            $rebateData['user_id'] = $skillUserId;
            UserInfo::money(+$skillPrice,$skillUserId,$goodsName.'平台订单收益到账',$order['id']);
            $rebateData['memo'] = $goodsName.'平台订单收益到账';
            self::create($rebateData);
            
            if($order['act_travel_price']>0)
            {
                UserInfo::money(+$order['act_travel_price'],$skillUserId,$goodsName.'订单车费结算到账',$order['id']);
                $rebateData['rebatetype'] = 2;
                $rebateData['num'] = $order['act_travel_price'];
                $rebateData['memo'] = $goodsName.'订单车费结算到账';
                self::create($rebateData);
            }
        }
        
        // 用户一级分佣
        $one_pid = model('app\api\model\service\UserInfo')->where(['user_id'=>$order['user_id']])->value('pid');
        
        
        if($one_pid) {
            $one_rebate = model('app\admin\model\service\info\Projectconfig')->value(['one_rebate']);
            $one_bonus = bcmul($order['settle_price'], bcdiv($one_rebate, 100, 8),2);
            // return $one_bonus;
            // 佣金明细 2025.05.30 客户要求逻辑不改只改文字 实际这是上级的奖励
            $res = UserInfo::bonus($one_bonus, $one_pid, '二级分佣奖励',$order['id']);
            // return $res;
        }
        // 用户二级分佣
        $two_pid = model('app\api\model\service\UserInfo')->where(['user_id'=>$one_pid])->value('pid');
        if($two_pid) {
            $two_rebate = model('app\admin\model\service\info\Projectconfig')->value(['two_rebate']);
            $two_bonus = bcmul($order['settle_price'], bcdiv($two_rebate, 100, 8),2);
            // return $two_bonus;
            // 佣金明细 2025.05.30 客户要求逻辑不改只改文字 实际这是上上级的奖励
            UserInfo::bonus($two_bonus, $two_pid, '一级分佣奖励',$order['id']);
        }
        // 服务上级分佣
        $skill_uid = model('app\api\model\service\Skill')->where(['id'=>$order['skill_id']])->value('user_id');
        $skill_pid = model('app\api\model\service\UserInfo')->where(['user_id'=>$skill_uid])->value('pid');
        if($skill_pid) {
            $one_rebate = model('app\api\model\service\Skill')->where(['id'=>$order['skill_id']])->value('one_rebate');
            $money = bcmul($order['settle_price'], bcdiv($one_rebate, 100, 8),2);
            // 余额明细
            UserInfo::skillBonus($money, $skill_pid, '下级分佣奖励',$order_id=$order['id']);
        }
        
        
        return true;
    }


    public static function getRebatePrice($percent,$settlePrice)
    {
        return bcmul(($percent*$settlePrice),0.01,2);
    }




}
